package org.ciudadanimal;

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.restfb.*;
import com.restfb.types.*;


@SuppressWarnings("serial")
public class CiudadanimalServlet extends HttpServlet {
	
    FacebookClient facebookClient;
    
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		resp.setContentType("text/plain");
		resp.getWriter().println("Hello, world");
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
			String signedRequest = (String) request.getParameter("signed_request");
			FacebookSignedRequest facebookSR = null;
			try {
				facebookSR = FacebookSignedRequest.getFacebookSignedRequest(signedRequest);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String oauthToken = facebookSR.getOauth_token();
			PrintWriter writer = response.getWriter();
			if(oauthToken == null) {
				
				response.setContentType("text/html");
				String authURL = "https://www.facebook.com/dialog/oauth?client_id="
									+ Constants.API_KEY + "&redirect_uri=https://apps.facebook.com/myoldfriends/&scope=";
				writer.print("<script> top.location.href='"	+ authURL + "'</script>");
				writer.close();

			}
			else {
				
				/*
				facebookClient = new DefaultFacebookClient(oauthToken);
				Connection<User> myFriends = facebookClient.fetchConnection("me/friends", User.class);
				writer.print("<table><tr><th>Photo</th><th>Name</th><th>Id</th></tr>");
				for (List<User> myFriendsList : myFriends) {
		
					for(User user: myFriendsList)
						writer.print("<tr><td><img src=\"https://graph.facebook.com/" + user.getId() + "/picture\"/></td><td>" + user.getName() +"</td><td>" + user.getId() + "</td></tr>");
		
				}
				writer.print("</table>");
				writer.close();
				*/
				response.sendRedirect("/jsp/welcome.jsp");
			}

		}
}
